import cairo
from math import pi

class drect:
    def __init__(self, drect_conf):
        self.WIDTH, self.HEIGHT = int(drect_conf['monitor width']), \
                                               int(drect_conf['monitor height'])
        self.frame_X = int(drect_conf['left border'])
        self.frame_Y = int(drect_conf['upper border'])
        self.dip_incr = int(drect_conf['dip net increment'])
        self.dir_incr =  int(drect_conf['dir net increment'])
               
        global surface, ctx        

        surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, self.WIDTH, self.HEIGHT)
        ctx = cairo.Context(surface)

        ctx.set_source_rgb (1, 1, 1)
        ctx.set_operator (cairo.OPERATOR_SOURCE)
        ctx.paint()
        
        
        #self.write_to_file()
        
    def draw_frame(self):
        
        #draw a frame
        ctx.set_source_rgb (0, 0, 0)
        ctx.set_line_width (2.0)

        ctx.move_to (self.frame_X, self.frame_Y)
        ctx.line_to (self.frame_X, self.HEIGHT-self.frame_Y)
        ctx.line_to (self.WIDTH - self.frame_X, self.HEIGHT - self.frame_Y)
        ctx.line_to (self.WIDTH - self.frame_X, self.frame_Y)
        ctx.line_to(self.frame_X, self.frame_Y)
        ctx.stroke()
        
        #draw a horizontal axes  
        ctx.set_source_rgb (0, 0, 0)
        ctx.set_line_width (0.5)
        
        ctx.select_font_face("Arial", 
                cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
        ctx.set_font_size(18)
         
        dip=0
        incr=(self.HEIGHT-40)/(180/self.dip_incr)        
        while dip<180/self.dip_incr: 
            ctx.move_to(20, 20+incr*dip)
            ctx.line_to(self.WIDTH-20, 20+incr*dip)
            ctx.stroke()
            dip=dip+1        
        ctx.move_to(0,20 +incr*3)
        ctx.show_text('90')
        ctx.move_to(0,20 +incr*6)
        ctx.show_text('0')
        ctx.move_to(0,20 +incr*0)
        ctx.show_text('0')
        ctx.stroke()
        

        #draw a vertical_axes
        ddir=0
        incr=(self.WIDTH-40)/(360/self.dir_incr)        
        while ddir<360/self.dir_incr: 
            ctx.move_to(20+incr*ddir, 20)
            ctx.line_to(20+incr*ddir, self.HEIGHT-20)
            ctx.stroke()            
            ctx.move_to(20+incr*ddir,15 )
            ctx.show_text(str(ddir*self.dir_incr))
            ctx.stroke()
            ddir=ddir+1       
      

        
        

    def draw_point(self, x, y,col=[1,0,0],radius=2,linewidth=1):
        ctx.set_source_rgb(col[0],col[1],col[2])
        ctx.set_line_width (linewidth)
        ctx.move_to(x + radius, y)
        ctx.arc(x, y, radius, 0, 2 * pi)
        ctx.fill()        
        
    def draw_points(self,data):
        for i in data:
            coords=self.get_coords(i[0],i[1])
    	    self.draw_point(coords[0], coords[1])
    	    
    def draw_rectangle(self,x1,y1,x2,y2,col=[0,0,0]):
        ctx.set_source_rgb(col[0],col[1],col[2])
        ctx.rectangle(x1,y1,x2,y2)
        ctx.fill()
    
    def get_coords(self,ddir,dip):
        x=self.frame_X+ddir*(self.WIDTH-2*self.frame_X)/360.0
        y=self.frame_Y+dip*(self.HEIGHT-2*self.frame_Y)/180.0
        return x,y        

    def density_mesh(self, data,size_of_gridwind):
        x_shift = size_of_gridwind * (self.WIDTH-2*self.frame_X)/360.0
        y_shift = size_of_gridwind * (self.HEIGHT-2*self.frame_Y)/180.0
        
        
        # searching of a maximum
        max_=[]
        for i in data:
            max_.append(max(i))
        max_data=max(max_)
        
        #drawing rectangles
        ddir=0
        while ddir<360.0/size_of_gridwind:
            ddip=0
            while ddip<180.0/size_of_gridwind:
                x, y = self.get_coords(ddir*size_of_gridwind,ddip*size_of_gridwind)
                col=1-data[ddir][ddip]*1.0/max_data
                self.draw_rectangle (x, y,x_shift,y_shift,[col,col,col])
                ddip+=1
            ddir+=1
        ctx.stroke()
    
    def draw_lines(self,data,size_of_gridwind,col=[0,1,0]):
        for j in data:
            for i in range(3,len(j)-1,2):
                y1,x1 = j[i-2], j[i-1]                
                y2,x2 = j[i], j[i+1]
                y1_,x1_ = self.get_coords((y1-1) * size_of_gridwind, \
                                (x1-1) * size_of_gridwind)
                y2_,x2_ = self.get_coords((y2-1) * size_of_gridwind, \
                                (x2-1) * size_of_gridwind)
                ctx.set_source_rgb(col[0],col[1],col[2])
                ctx.set_line_width (1)            
                ctx.move_to(x1_,y1_)
                ctx.line_to(x2_,y2_)
                ctx.stroke ()
                
    
    def write_to_file(self, file_name):
        self.draw_frame()
        try:
            surface.write_to_png(file_name)
        except:
            print '    Error while writing to file'
